package com.besttone.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrDocumentList;

import com.besttone.bean.ShortlinkBean;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.ShortlinkBrowseRecord2SolrUtil;
import com.besttone.util.SolrObject;
import com.jfinal.core.Controller;

public class ShortlinkAction extends Controller {
    private static Log _log = LogFactory.getLog(ShortlinkAction.class);

    /**
     * 查询
     */
    @SuppressWarnings("rawtypes")
    public void queryShortlink() {
	String streaming_no = getPara("streaming_no");
	String sp_account = getPara("sp_account");
	String client_ip = getPara("client_ip");
	String client_phone = getPara("client_phone");
	String short_code = getPara("short_code");
	String request_source = getPara("request_source");
	String cust_code = getPara("cust_code");
	String template_code = getPara("template_code");
	String business_phone = getPara("business_phone");
	String client_browse_time_from = getPara("client_browse_time_from");
	String client_browse_time_to = getPara("client_browse_time_to");
	String current_page = StringUtils.isBlank(getPara("current_page")) ? "1"
		: getPara("current_page");// 当前页数
	String per_page_size = StringUtils.isBlank(getPara("per_page_size")) ? "10"
		: getPara("per_page_size");// 每页条数，默认10
	_log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
		+ sp_account + ",client_ip=" + client_ip + ",client_phone="
		+ client_phone + ",client_browse_time_from="
		+ client_browse_time_from + ",client_browse_time_to="
		+ client_browse_time_to + ",per_page_size=" + per_page_size
		+ ",request_source=" + request_source + ",cust_code="
		+ cust_code + ",template_code=" + template_code
		+ ",business_phone=" + business_phone);
	if (StringUtils.isBlank(client_browse_time_from)) {
	    setAttr("result_code",
		    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
	    setAttr("result_msg", "查询开始时间不能为空");
	    renderJson();
	    return;
	}
	StringBuffer strBuf = new StringBuffer();
	if (StringUtils.isNotBlank(client_browse_time_from)) {
	    client_browse_time_from = StringUtils.trim(client_browse_time_from);
	    strBuf.append(" (client_browse_time:[");
	    strBuf.append(client_browse_time_from);
	    strBuf.append(" TO ");
	    if (StringUtils.isNotBlank(client_browse_time_to)) {
		strBuf.append(StringUtils.trim(client_browse_time_to));
	    } else {
		strBuf.append("*");
	    }
	    strBuf.append("]) ");
	    if (StringUtils.isNotBlank(client_browse_time_to)) {
		client_browse_time_to = StringUtils.trim(client_browse_time_to);
		strBuf.append(" AND (client_browse_time:[* TO ");
		strBuf.append(StringUtils.trim(client_browse_time_to));
		strBuf.append("]) ");
	    }
	    if (StringUtils.isNotBlank(client_ip)) {
		client_ip = StringUtils.trim(client_ip);
		strBuf.append(" AND (client_ip:");
		strBuf.append(client_ip);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(client_phone)) {
		client_phone = StringUtils.trim(client_phone);
		strBuf.append(" AND (client_phone:");
		strBuf.append(client_phone);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(short_code)) {
		short_code = StringUtils.trim(short_code);
		strBuf.append(" AND (short_code:");
		strBuf.append(short_code);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(request_source)) {
		request_source = StringUtils.trim(request_source);
		strBuf.append(" AND (request_source:");
		strBuf.append(request_source);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(cust_code)) {
		cust_code = StringUtils.trim(cust_code);
		strBuf.append(" AND (cust_code:");
		strBuf.append(cust_code);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(template_code)) {
		template_code = StringUtils.trim(template_code);
		strBuf.append(" AND (template_code:");
		strBuf.append(template_code);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(business_phone)) {
		business_phone = StringUtils.trim(business_phone);
		strBuf.append(" AND (business_phone:");
		strBuf.append(business_phone);
		strBuf.append(") ");
	    }
	}
	String querySql = strBuf.toString();
	_log.info("solr查询语句:" + querySql);
	try {
	    setAttr("streaming_no", streaming_no);
	    SolrDocumentList docList = ShortlinkBrowseRecord2SolrUtil.search(
		    querySql, Integer.valueOf(current_page),
		    Integer.valueOf(per_page_size));
	    setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
	    List list = SolrObject.toBeanList(docList, ShortlinkBean.class);
	    setAttr("result_msg", list);// 返回详情数据
	    setAttr("result_num", docList.getNumFound());
	} catch (Exception e) {
	    _log.info("solr查询异常:" + e);
	    setAttr("result_code", EnumAPIResultCode.SYSTEM_ERROR.getValue());
	    setAttr("result_msg", EnumAPIResultCode.SYSTEM_ERROR.getName());
	}
	renderJson();
	return;
    }
}
